package com.example.cwproject.controller;

import cn.hutool.core.io.FileUtil;
import com.example.cwproject.common.AuthAccess;
import com.example.cwproject.common.Result;
import com.example.cwproject.entity.Post;
import com.example.cwproject.service.PostService;
import com.example.cwproject.service.UserService;
import com.example.cwproject.service.serviceImpl.PostServiceImpl;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Getter;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {

    @Value("${ip}")  //默认值
    String ip;

    @Value("${server.port}")
    String port;

    @Autowired
    private PostService postService;

    //添加帖子
    @PostMapping("/add")
    public String addPost(@RequestBody Post post){
        Integer PostID = postService.addPost(post);
        return String.valueOf(PostID);
    }

    //添加帖子的配图
    @PostMapping("/uploadPostImage")
    public String uploadPostImage(MultipartFile file, Integer postID) throws IOException {


        String originalFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(originalFilename);
        String extName = FileUtil.extName(originalFilename);
        String projectPath = System.getProperty("user.dir");
        String filesPath = projectPath + "/PostImage";
        String fileRealPath = filesPath + File.separator + originalFilename;

        File saveFile = new File(fileRealPath);
        if (!saveFile.getParentFile().exists()) {
            saveFile.getParentFile().mkdirs();
        }
        if (saveFile.exists()) {
            originalFilename = System.currentTimeMillis() + "_" + mainName + "." + extName;
            saveFile = new File(filesPath + File.separator + originalFilename);
        }
        file.transferTo(saveFile);
        String url = "http://" + ip + ":" + port + "/post/download/" + originalFilename;
        System.out.println(url + "\n");
        String imagUrl = postService.uploadImage(url, postID);

        return imagUrl;
    }

    @AuthAccess
    @GetMapping("/download/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse response) throws IOException {

        String projectPath = System.getProperty("user.dir");
        String filesPath = projectPath + "/PostImage" + File.separator + filename; //本地目录
        if(!FileUtil.exist(filesPath)){     //通过FileUtil方法， 判断如果当前上传的文件已存在，就重命名文件名称
            return;
        }
        byte[] bytes = FileUtil.readBytes(filesPath);  //文件的字节流数组
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();;
        outputStream.close();
    }

    //删除帖子
    @DeleteMapping("/delete")
    public Result deletePost(Integer postID){

        postService.delete(postID);
        return Result.success();
    }

    //查询所有帖子
    @GetMapping("/selectAllPost")
    public List<Post> selectAll(){
        return postService.selectAllPost();
    }

    //查询帖子  ---根据用户名
    @GetMapping("/selectPostByUser")
    public List<Post> selectByUser(String userName){
        return postService.selectByUser(userName);
    }

    //查询帖子   ----根据邮箱
    @GetMapping("/selectPostByEmail")
    public List<Post> selectByEmail(String email){
        return postService.selectByEmail(email);
    }

}
